﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title = "List";
    Layout = "~/views/Shared/_ProductLayout.cshtml";
}

<table class="table">

    <tr>
        <th>
            @T("Admin_Product_List_ProductName_Title")
        </th>
        <th>
            @T("Admin_Product_List_ProductPictureUrl_Title")
        </th>
        <th>
            
               @T("Admin_Product_List_IsPublished_Title")
        </th>
        <th>
            
        </th>
    </tr>
    
    <!-- ko foreach: ProductList -->
    <tr>
        <td>
             <span data-bind="text:ProductName"></span>
        </td>
        <td>
            <img data-bind="attr:{src: PictureUrl}" /> 
            
        </td>
        <td>
                   
        <span data-bind="text:IsPublished"></span>
        </td>
        <td>
            <a data-bind="attr: { href: '/Product/Edit?productId='+ProductId() }">@T("Admin_Product_List_Edit_Title")</a>&nbsp;
           <a data-bind="click:$root.DeleteProduct">@T("admin_product_delete_title")</a>
        </td>
    </tr>

    <!-- /ko -->

</table>


<a href="@Url.Action("Create", "Product")" class="btn btn-primary"> @T("Admin_Product_List_CreateProduct")</a>




@{
    var model = ViewBag.Model;
    var jsonStr = new HtmlString(new JavaScriptSerializer().Serialize(model));
}


@section scripts
{
    <script>
        var init = @jsonStr;

       

        function ProductListModel(productId, ispublished, productname, pictureUrl) {

            var self = this;
            self.ProductId =ko.observable(productId);
            self.IsPublished = ko.observable(ispublished);
            self.ProductName = ko.observable(productname);
            self.PictureUrl = ko.observable(pictureUrl);
        }

        function ProductSearchModel() {

            var self = this;
            
            var tempProductList= $.isEmptyObject(init.ProductListModels)? []: $.map(init.ProductListModels, function (a) {
                return new ProductListModel(a.ProductId,a.IsPublished, a.ProductName, a.ProductListItemProductPicture.PicUrl);
            });
            self.ProductList = ko.observableArray(tempProductList);


            self.DeleteProduct = function(p) {
                if (confirm('@T("admin.product.delete.confirm")')) {
                      var data = {
                          ProductId: p.Id()
                      };
                      $.ajax({
                          url: "/api/ProductApi/DeleteProduct",
                          type: 'post',
                          data: ko.toJSON(data),
                          contentType: 'application/json',
                          success: function(result) {

                              if (result.Status == "0") {
                                  self.ProductList.remove(p);
                                  toastr.success(result.Message);
                              } else {
                                  toastr.error(result.Message);
                              }
                          }
                      });
                  }
                
              };
        }

        var vm = new ProductSearchModel();
        ko.applyBindings(vm);
        
    </script>
}